package com.vortex.cloud.zhsw.xinyang.manager;

import com.vortex.cloud.sdk.api.dto.ums.UserDataPermissionVO;
import com.vortex.cloud.sdk.api.service.IUmsService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.Set;

/**
 * @author: bds
 */
@Component
public class DataPermissionService {
    @Resource
    private IUmsService umsService;

    public void getDataPermission(String userId, @NotNull Set<String> permissionDeptIds, @NotNull Set<String> permissionDivisionIds) {
        if (StringUtils.isEmpty(userId)) {
            return;
        }
        UserDataPermissionVO dataPermission = umsService.getDataPermission(userId);
        boolean filterFlag = (null == dataPermission) ;
        if (filterFlag) {
            return;
        }
        if(!(CollectionUtils.isEmpty(dataPermission.getDivisionIds()) || "NONE".equals(dataPermission.getDivisionScope()))){
            permissionDivisionIds.addAll(dataPermission.getDivisionIds());
        }
        if(!(CollectionUtils.isEmpty(dataPermission.getDeptOrgIds()) || "NONE".equals(dataPermission.getDeptOrgScope()))){
            permissionDeptIds.addAll(dataPermission.getDeptOrgIds());
        }
    }
}
